package com.fita.admin.service.authority.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.support.QueryFilter;
import com.fita.admin.dao.PrincipalDao;
import com.fita.admin.entity.security.Login;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.authority.PrincipalService;
import com.fita.entity.principal.District;
import com.fita.entity.principal.Principal;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 
 * @author dan
 *
 */
@Service("principalService")
public class PrincipalServiceImpl extends DataServiceImpl<Principal> implements PrincipalService{
    
    @Dao
    private PrincipalDao principalDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#count(com.fita.admin.entity.security.Login)
     */
    public int count(Login login) {
        if(login.isSaleDirector()) {
            return principalDao.count("SALE_USER", login.getDistrict());
        } else {
            return principalDao.count();
        }
    }

    /*
     * (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#list(com.fita.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
     */
    public List<Principal> list(Login login, Page page) {
        if(login.isSaleDirector()) {
            return principalDao.list("SALE_USER", login.getDistrict(), page.getBeginIndex(), page.getMaxResultRecords());
        } else {
            return principalDao.listByPage(page.getBeginIndex(), page.getMaxResultRecords());
        }
    }

    /*
     * (non-Javadoc)
     * @see com.fita.admin.service.PrincipalService#loadByName(java.lang.String)
     */
    public Principal loadByName(String name) {
        return principalDao.load(name);
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#createPrincipal(com.fita.entity.principal.Principal, com.fita.entity.principal.District)
     */
    public void createPrincipal(Principal principal,District district) {
        // 1. check create principal
        Principal principal1=principalDao.load(principal.getUsername());
        if(principal1 != null) {
            throw BusinessException.newInstance("400", "新增用户已存在");
        }
        // 2. do save principal
        genericDaoSupport.save(principal.initliaze(principal,district));
        
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#updatePrincipal(com.fita.entity.principal.Principal, com.fita.entity.principal.District)
     */
    public void updatePrincipal(Principal principal, District district) {
        principal.evaluateDistrict(district);
        genericDaoSupport.update(principal);
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#restPassword(com.fita.entity.principal.Principal)
     */
    public void resetPassword(Principal principal) {
        principal.resetPassword();
        genericDaoSupport.update(principal);
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#activate(com.fita.entity.principal.Principal)
     */
    public void activate(Principal principal) {
        principal.activate();
        genericDaoSupport.update(principal);
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#freeze(com.fita.entity.principal.Principal)
     */
    public void freeze(Principal principal) {
        principal.freeze();
        genericDaoSupport.update(principal);
    }
    
    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#countSearch(java.lang.Class, com.demo2do.core.persistence.support.QueryFilter)
     */
    public int countSearch(Class<Principal> clazz, QueryFilter queryFilter) {
        return genericDaoSupport.searchForInt("SELECT COUNT(*) FROM " + ClassUtils.getShortClassName(clazz) + queryFilter.whereSentence(), queryFilter.parameters());
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#listSaleUser()
     */
    public List<Principal> listSaleUser() {
        return principalDao.listSaleUser("SALE_USER");
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#changePassword(com.fita.entity.principal.Principal, java.lang.String)
     */
    public void changePassword(Principal principal, String password) {
        genericDaoSupport.update(principal.changePassword(password));
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.PrincipalService#list(java.util.List, com.fita.entity.principal.District)
     */
    public List<Principal> list(List<String> authorities, District district) {

        if (CollectionUtils.isEmpty(authorities)) {
            
            List<Principal> principals = principalDao.list(district);
            
            return principals;
        }
        List<Principal> principals = principalDao.list(authorities, district);
        
        return principals;
    }

}
